package com.zengde.ai_sandbox.controller;

import com.zengde.ai_sandbox.service.OssService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/files")
@RequiredArgsConstructor
public class FileUploadController {

    private final OssService ossService;

    /**
     * 上传文件到临时目录
     * @param file 文件
     * @return 上传结果
     */
    @PostMapping("/upload")
    public ResponseEntity<Map<String, String>> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String objectName = ossService.uploadToTemp(file);
            Map<String, String> response = new HashMap<>();
            response.put("message", "文件上传成功");
            response.put("objectName", objectName);
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", "文件上传失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 将文件从临时目录移动到永久目录
     * @param objectName 对象名称
     * @return 移动结果
     */
    @PostMapping("/move-to-product")
    public ResponseEntity<Map<String, String>> moveToProduct(@RequestParam("objectName") String objectName) {
        try {
            ossService.moveToProduct(objectName);
            Map<String, String> response = new HashMap<>();
            response.put("message", "文件已成功移动到product目录");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", "文件移动失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 